var DownloadBinning = {
  prefs: null,

  onLoad: function(event) {
    
    var main = Components.classes['@downloadbinning.net/main;1']
                                .getService(Components.interfaces.nsIDownloadBinning);
    
  	var domain = document.getElementById("source").value;
  	var filename = document.getElementById("location").value;

    var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);    
    var destination = main.getLocation(domain, filename);
    
    // if match one of the rules, change the browser preference "browser.download.dir" 
    // else, leave the browser preference the same
    if (destination != null) {
      pref.setCharPref("browser.download.dir", destination);
      pref.setCharPref("browser.download.lastDir", destination);
    } else {
    }

  },
 
};

function bd_onLoad(event) {
  try {
    DownloadBinning.onLoad();
  } catch (e) {
    throw e;
  }
}

window.addEventListener("load", bd_onLoad, false);
